package cn.fl.system.controller;

import cmm.comm.facade.framework.api.ISysRegionApiService;
import cmm.comm.facade.framework.condition.SysRegionQueryCondition;
import cmm.comm.facade.framework.dto.SysRegionDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.condition.SysOrgRegRQC;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.constant.SysLogConstant;
import cn.fl.system.service.ISysOrgRegRService;
import cn.fl.system.service.ISysOrgService;
import cn.fl.system.service.ISysRegionService;
import cn.fl.system.vo.SysOrgRegRVO;
import cn.fl.system.vo.SysOrgVO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import net.sf.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Description: 组织架构-区域信息关系表 控制层
 */
@Controller
@RequestMapping("/system/sysOrgRegR")
public class SysOrgRegRController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(SysOrgRegRController.class);

	/**
	 * SysOrgRegR 服务层BEAN
	 */
	@Autowired
	private ISysOrgRegRService sysOrgRegRService;

	@Autowired
	private ISysOrgService sysOrgService ;

	@Autowired
	private ISysRegionApiService sysRegionApiService ;

	/**
	 * 初始化页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toListPage", "加载列表页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		ModelAndView view = new ModelAndView("system/sysOrgRegR/sysSysOrgRegRList");
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toListPage", "加载列表页面.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return view;
	}
	
	/**
	 * 到新增修改页面
	 */
	@RequestMapping(value = "/toAddPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toAddPage(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toAddPage", "加载新增.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		ModelAndView view = new ModelAndView("system/sysOrgRegR/sysOrgRegRAdd");
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toAddPage", "加载新增.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 到修改页面
	 */
	@RequestMapping(value = "/toModifyPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toAddPage", "加载修改页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		
		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("system/sysOrgRegR/sysOrgRegRModify");
		view.addObject("id", id);
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toAddPage", "加载修改.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return view;
	}
	
	/**
	 * 到详情页面
	 */
	@RequestMapping(value = "/toDetailPage.do", method = { RequestMethod.GET})
	public ModelAndView toDetailPage(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toDetailPage", "加载例子详细页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		
		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("system/sysOrgRegR/sysOrgRegRDetail");
		view.addObject("id", id);
		
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toDetailPage", " 加载例子详细页面.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return view;
	}

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "initList", "加载列表页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());

		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String searchName = request.getParameter("demo.name");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		SysOrgRegRQC qc = new SysOrgRegRQC();
		if (StringUtils.isNotEmpty(searchName)) {
			//TODO SOMTHING
		}
		page.setCondition(qc);

		//TODO SOMETHING
		Pager pager = new Pager();//this.sysOrgRegRService.searchListPage(page);
		List<SysOrgRegRVO> list = (List<SysOrgRegRVO>) pager.getResultList();
		ListPageVO<SysOrgRegRVO> listPage = new ListPageVO<SysOrgRegRVO>(list, pager.getTotalCount());
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "initList", "加载列表页面.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}
	
	/**
	 * 新增
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "add", "新增对象.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		
		String name = request.getParameter("name");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "新增成功!");
		SysOrgRegRVO entity = null;
		try {
			entity = new SysOrgRegRVO();
			//TODO ADD PARAM
			Long id = this.sysOrgRegRService.add(entity);
			result.setData(id);
			BaseLogUtils.info(logger, "add", "新增数据.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "add", "新增对象失败,name["+name+"].", CurrentThreadContext.getCurrentUserName(), ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.error(logger, "add", "新增对象失败,name["+name+"].", CurrentThreadContext.getCurrentUserName(), ex);
		}
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "add", "新增对象.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		
		return result;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "modify", "修改对象.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "修改成功!");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("数据不存在!");
			BaseLogUtils.info(logger, "modify", "修改数据,主键不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		SysOrgRegRVO entity = null;
		try {
			entity = new SysOrgRegRVO();
			entity.setId(Long.valueOf(id));
			//entity.setName(name);
			//TODO ADD PARAM
			boolean success = this.sysOrgRegRService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("修改失败!");
				BaseLogUtils.info(logger, "modify", "修改失败.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
			}
			BaseLogUtils.info(logger, "modify", "修改成功.[id="+id+"] DATA:" + JSONObject.toJSONString(entity), CurrentThreadContext.getCurrentUserName());
		} catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "modify", "修改对象失败,id["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.error(logger, "modify", "修改对象失败,id["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		}
		
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "modify", "修改对象.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());

		return result;
	}

	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/removeById.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "remove", "移除对象.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "删除成功!");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("主键不能为空!");
			return result;
		}
		try {
			this.sysOrgRegRService.removeById(Long.valueOf(id));
			BaseLogUtils.info(logger, "removeById", "删除数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "remove", "移除对象失败,主键["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.error(logger, "remove", "移除对象失败,主键["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		}
		
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "remove", "移除对象.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());

		return result;
	}

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "getById", "根据主键获取对象.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "操作成功!");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("主键不能为空!");
			BaseLogUtils.error(logger, "getById", "获取数据,主键不存在.[id=" + id + "]", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		
		try {
			SysOrgRegRVO vo = this.sysOrgRegRService.selectById(Long.valueOf(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("数据不存在!");
				BaseLogUtils.info(logger, "getById", "获取数据,数据不存在.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
			}
			result.setData(vo);
			BaseLogUtils.info(logger, "getById", "获取数据.[id="+id+"]", CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getById", "根据主键获取对象失败,主键["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.error(logger, "getById", "根据主键获取对象失败,主键["+id+"].", CurrentThreadContext.getCurrentUserName(),ex);
		}
		
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "getById", "根据主键获取对象.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());

		return result;
	}

	/**
	 * 通过组织架构id得到资源树初始化数据
	 *
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getTreeByOrgId.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getTreeByRoleId(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String orgIdStr = request.getParameter("orgId");
		Long orgId = null;
		if(StringUtils.isNotEmpty(orgIdStr)){
			orgId =Long.valueOf(orgIdStr);
		}

		try {
			net.sf.json.JSONObject jsonObject = this.sysOrgRegRService.searchRegionsByOrgId(orgId);
			return jsonObject.get("data");
		}catch(Exception ex) {
			BaseLogUtils.error(logger, "getTreeByRoleId", "通过角色ID获取资源树初始化数据失败,系统错误.orgId:"+orgIdStr+".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return null;
	}

	/**
	 * 通过资源名称模糊得到资源树
	 *
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getByRegionNm.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getByRegionNm(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String regionNm = request.getParameter("regNm");
		String[] regIdArr = request.getParameterValues("RESOURCE_IDs[]");
		BaseLogUtils.info(logger, "getByRegionNm", "加载区域信息树.",CurrentThreadContext.getCurrentUserName());
		List<String> regionIdList = new ArrayList<String>();
		if(regIdArr!=null && regIdArr.length>0){
			Collections.addAll(regionIdList, regIdArr);
		}
		List<Long> ids = new ArrayList<Long>();
		if(CollectionUtils.isNotEmpty(regionIdList)){
			for (String idStr : regionIdList) {
				ids.add(Long.valueOf(idStr));
			}
		}

		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		SysRegionQueryCondition condition = new SysRegionQueryCondition();
		if(StringUtils.isNotBlank(regionNm)){
			condition.setRgnNm(regionNm);
		}
		condition.setSysCd(CurrentThreadContext.getCurrentSysCd());
		try {
			JSONArray jsonObject = this.sysOrgRegRService.searchOrgRegRTree(ids, condition);
			if(jsonObject==null){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
				BaseLogUtils.info(logger, "getByResNm", "获取区域数据不存在.", CurrentThreadContext.getCurrentUserName());
			}else{
				BaseLogUtils.info(logger, "getByResNm", "加载区域数据树成功.", CurrentThreadContext.getCurrentUserName());
			}
			result.setData(jsonObject);
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getByResNm", "获取区域数据失败.ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "getByResNm", "获取区域数据失败,系统错误.ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}

	/**
	 * 新增 组织架构授权
	 */
	@RequestMapping(value = "/addRegToOrg.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object addRegToOrg(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		BaseLogUtils.info(logger,"addRegToOrg","请求参数："+this.getRequestParameter(request));
		String sysCd = request.getParameter("sysCd");
		String orgIdStr = request.getParameter("orgId");
		String[] regIdArr = request.getParameterValues("regionIdArr[]");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		Long orgId =null;
		List<SysOrgRegRVO> sysOrgRegRVOList = new ArrayList<SysOrgRegRVO>();
		if(StringUtils.isBlank(orgIdStr)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_AUTH_ROLE_ID_NOT_EMPTY);
			BaseLogUtils.info(logger, "add", "组织架构授权,组织架构ID参数为空！", CurrentThreadContext.getCurrentUserName());
			return result;
		}else{
			orgId =Long.valueOf(orgIdStr);
		}
		SysOrgVO sysOrgVO = this.sysOrgService.selectById(orgId);
		if(null == sysOrgVO){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("组织架构信息为空！");
			BaseLogUtils.info(logger, "add", "组织架构授权,组织架构信息为空！", CurrentThreadContext.getCurrentUserName());
			return result;
		}
		//String[] resIdArr = request.getParameterValues("resIdArr[]");
		List<String> regIdList = new ArrayList<String>();
		if(regIdArr!=null && regIdArr.length>0){
			Collections.addAll(regIdList, regIdArr);
		}
		if(CollectionUtils.isNotEmpty(regIdList)){
			List<Long> regionIdList = new ArrayList<>(regIdList.size());
			for (String regIds : regIdList) {
				regionIdList.add(Long.valueOf(regIds));
			}
			SysRegionQueryCondition queryCondition = new SysRegionQueryCondition();
			queryCondition.setIdList(regionIdList);
			queryCondition.setSysCd(CurrentThreadContext.getCurrentSysCd());
			List<SysRegionDTO> sysRegionDTOs = this.sysRegionApiService.searchRegionList(queryCondition);
			Map<Long,String> rgnIdRgnCdMap = null ;
			if(CollectionUtils.isNotEmpty(sysRegionDTOs)){
				rgnIdRgnCdMap = new HashMap<>(sysRegionDTOs.size());
				for(SysRegionDTO dto : sysRegionDTOs){
					rgnIdRgnCdMap.put(dto.getId(),dto.getRgnCd());
				}
			}
			if(StringUtils.isEmpty(sysCd)){
				if(StringUtils.isNotEmpty(sysOrgVO.getSysCd())){
					sysCd = sysOrgVO.getSysCd() ;
				}else{
					sysCd = CurrentThreadContext.getCurrentSysCd();
				}
			}
			for (String regIds : regIdList) {
				Long regId = Long.valueOf(regIds);
				SysOrgRegRVO sysOrgRegRVO = new SysOrgRegRVO();
				sysOrgRegRVO.setRgnId(regId);
				sysOrgRegRVO.setOrgCd(sysOrgVO.getOrgCd());
				sysOrgRegRVO.setSysCd(sysCd);
				if(CollectionUtils.isMapNotEmpty(rgnIdRgnCdMap) && rgnIdRgnCdMap.containsKey(regId)){
					sysOrgRegRVO.setRgnCd(rgnIdRgnCdMap.get(regId));
				}
				sysOrgRegRVO.setOrgId(orgId);
				sysOrgRegRVO.setCrtTm(new Date());
				sysOrgRegRVO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
				sysOrgRegRVOList.add(sysOrgRegRVO);
			}
		}
		try {
			boolean addAuth = this.sysOrgRegRService.addOrgRegionR(orgId, sysOrgRegRVOList);
			if(!addAuth){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_OPERATE_FAIL);
				BaseLogUtils.info(logger, "add", "组织架构授权失败.orgId:"+orgIdStr+".regionIds:"+ JSON.toJSONString(regIdArr), CurrentThreadContext.getCurrentUserName());
				return result;
			}
			BaseLogUtils.info(logger, "add", "组织架构授权成功.orgId:" + orgIdStr + ".regionIds:" + JSON.toJSONString(regIdArr), CurrentThreadContext.getCurrentUserName());
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "add", "组织架构授权失败.orgId:"+orgIdStr+".regionIds:"+JSON.toJSONString(regIdArr)+".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "add", "组织架构授权失败,系统错误..orgId:"+orgIdStr+".regionIds:"+JSON.toJSONString(regIdArr)+".ERROR:" + ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}

		return result;
	}

	/**
	 * 得到一条记录
	 *
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getRegionByOrgId.do", method = { RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public Object getRegionByOrgId(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "getRegionByOrgId", "根据组织架构ID获取对象.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());

		String orgId = request.getParameter("orgId");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "操作成功!");
		if(StringUtils.isEmpty(orgId)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("组织架构ID不能为空!");
			BaseLogUtils.error(logger, "getById", "获取数据,组织架构ID不存在.[orgId=" + orgId + "]", CurrentThreadContext.getCurrentUserName());
			return result;
		}

		try {
			net.sf.json.JSONObject jsonObject = this.sysOrgRegRService.searchRegionsByOrgId(Long.valueOf(orgId));
			if(jsonObject == null){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
				BaseLogUtils.info(logger, "getByResNm", "获取区域数据不存在.", CurrentThreadContext.getCurrentUserName());
			}else{
				BaseLogUtils.info(logger, "getByResNm", "加载区域数据树成功.", CurrentThreadContext.getCurrentUserName());
			}
			result.setData(jsonObject);
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "getById", "根据组织架构ID获取对象失败,组织架构ID["+orgId+"].", CurrentThreadContext.getCurrentUserName(),ex);
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.error(logger, "getById", "根据组织架构ID获取对象失败,组织架构ID["+orgId+"].", CurrentThreadContext.getCurrentUserName(),ex);
		}

		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "getById", "根据组织架构ID获取对象.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());

		return result;
	}
}

